#!/user/local/bin/python3
# -*- coding: utf-8 -*-
import sys
import os
from typing import Callable

from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMessageBox, QLabel, QLineEdit, QTextEdit,
                             QRadioButton, QButtonGroup, QScrollArea, QScrollBar, QFileDialog)
from PyQt5.QtGui import (QFont)
from PyQt5.QtCore import Qt, pyqtSignal

import utils


class DefaultPushButton(QPushButton):
    def __init__(self, parents):
        super().__init__(parents)
        self.setStyleSheet("QPushButton{color:black; background-color:rgb(220, 220, 220)}"
                           "QPushButton:hover{border:2px solid grey}"
                           "QPushButton:pressed{background-color:rgb(100, 100, 100); color:white}"
                           "QPushButton{border:1px solid grey; border-radius:6px}")


class DefaultLineEdit(QLineEdit):
    def __init__(self, parents):
        super().__init__(parents)
        self.setFont(QFont("Microsoft YaHei", 12))
        self.setAlignment(Qt.AlignCenter)
        self.setAttribute(Qt.WA_MacShowFocusRect, False)
        self.setStyleSheet("QLineEdit{border-radius:10px; padding:2px 2px}")


class MainWidgets(QWidget):
    root_dir_path = ''

    def __init__(self):
        super().__init__()

        # init ui

        # set window attribute
        self.setWindowFlag(Qt.WindowCloseButtonHint)
        self.setFixedSize(600, 360)
        self.setWindowTitle('选择文件夹')

        # set widget ui
        self.select_root_dir_button = DefaultPushButton(self)
        self.select_root_dir_button.setText('选择文件夹')
        self.select_root_dir_button.setGeometry(35, 30, 80, 20)

        self.root_dir_path_label = QLabel(self)
        self.root_dir_path_label.setGeometry(130, 30, 500, 20)
        # self.root_dir_path_label.setText('/test/test/test/test')

        self.open_root_dir_button = DefaultPushButton(self)
        self.open_root_dir_button.setText('打开文件夹')
        self.open_root_dir_button.setGeometry(35, 80, 80, 20)

        self.open_xlsx_file_button = DefaultPushButton(self)
        self.open_xlsx_file_button.setText('打开xlsx文件')
        self.open_xlsx_file_button.setGeometry(230, 80, 100, 20)

        self.export_xlsx_file_button = DefaultPushButton(self)
        self.export_xlsx_file_button.setText('导出xlsx文件')
        self.export_xlsx_file_button.setGeometry(430, 80, 100, 20)

        # set control slot
        self.select_root_dir_button.clicked.connect(self._slot_select_root_dir)
        self.open_root_dir_button.clicked.connect(self._slot_open_root_dir)
        self.open_xlsx_file_button.clicked.connect(self._slot_open_xlsx_file)
        self.export_xlsx_file_button.clicked.connect(self._slot_export_xlsx_file)

    def _slot_select_root_dir(self):
        root_dir_path = QFileDialog.getExistingDirectory(None, '请选择文件夹路径', os.path.join('.', 'test'))
        # the return value is a url path, spilt with '/'

        print('select root dir: ' + root_dir_path)

        if root_dir_path != '':
            self.root_dir_path = root_dir_path
            self.root_dir_path_label.setText(self.root_dir_path)

    def _slot_open_root_dir(self):
        if self.root_dir_path != '':
            utils.reveal_in_explorer(self.root_dir_path, 'dir')

    def _slot_open_xlsx_file(self):
        xlsx_file = QFileDialog.getOpenFileName(None, '请选择xlsx文件路径',
                                                os.path.join(self.root_dir_path, 'export'),
                                                'Xlsx Files (*.xlsx);All Files (*)')[0]

        print('select xlsx file: ' + xlsx_file)

        utils.reveal_in_explorer(xlsx_file, 'file')

    def _slot_export_xlsx_file(self):
        QMessageBox.information(self, "导出", "导出成功")


def test():
    app = QApplication(sys.argv)
    m = MainWidgets()
    m.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    test()
